export class paginationInterfacesImpl {
  data: any;
  page_current: number;
  page_size: number;
  total: number;
}

const { getDate } = require('../utils/tool');

export const pagination = async (
  pageParam,
  Repository,
  createQueryBuilderName,
) => {
  // if (createQueryBuilderName == 'bookmark') {
  //
  // }
  // .findOne({
  //         category_id: pageParam.category_id
  //     })
  const queryData = {
    category_id: pageParam.category_id,
  };
  if (!pageParam.category_id) delete queryData.category_id;
  const page_size = pageParam.page_size ? pageParam.page_size : 10;
  const page_current = pageParam.page_current ? pageParam.page_current : 1;
  const community = await Repository.createQueryBuilder('user')
    .addOrderBy('sort', 'DESC')
    .where(queryData)
    .skip(page_size * (page_current - 1))
    .take(page_size)
    .getMany();
  const total = await Repository.createQueryBuilder(
    createQueryBuilderName,
  ).getCount();
  community.forEach((t) => {
    t.create_time = getDate(t.create_time);
    t.updated_time = getDate(t.updated_time);
  });
  const data = {
    page_size,
    page_current,
    total,
    data: community,
  };

  return data;
};
